<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 8.10: Approximate linear discrimination via linear programming</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch08_geometric_probs/html/svm_1.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 8.10: Approximate linear discrimination via linear programming</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.6.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original by Lieven Vandenberghe</span>
<span class="comment">% Adapted for CVX by Joelle Skaf - 10/16/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% The goal is to find a function f(x) = a'*x - b that classifies the non-</span>
<span class="comment">% separable points {x_1,...,x_N} and {y_1,...,y_M} by allowing some</span>
<span class="comment">% misclassification. a and b can be obtained by solving the following</span>
<span class="comment">% problem:</span>
<span class="comment">%           minimize    1'*u + 1'*v</span>
<span class="comment">%               s.t.    a'*x_i - b &gt;= 1 - u_i        for i = 1,...,N</span>
<span class="comment">%                       a'*y_i - b &lt;= -(1 - v_i)     for i = 1,...,M</span>
<span class="comment">%                       u &gt;= 0 and v &gt;= 0</span>

<span class="comment">% data generation</span>
n = 2;
randn(<span class="string">'state'</span>,2);
N = 50; M = 50;
Y = [1.5+0.9*randn(1,0.6*N), 1.5+0.7*randn(1,0.4*N);
     2*(randn(1,0.6*N)+1), 2*(randn(1,0.4*N)-1)];
X = [-1.5+0.9*randn(1,0.6*M),  -1.5+0.7*randn(1,0.4*M);
      2*(randn(1,0.6*M)-1), 2*(randn(1,0.4*M)+1)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;

<span class="comment">% Solution via CVX</span>
cvx_begin
    variables <span class="string">a(n)</span> <span class="string">b(1)</span> <span class="string">u(N)</span> <span class="string">v(M)</span>
    minimize (ones(1,N)*u + ones(1,M)*v)
    X'*a - b &gt;= 1 - u;
    Y'*a - b &lt;= -(1 - v);
    u &gt;= 0;
    v &gt;= 0;
cvx_end

<span class="comment">% Displaying results</span>
linewidth = 0.5;  <span class="comment">% for the squares and circles</span>
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
tt = linspace(t_min-1,t_max+1,100);
p = -a(1)*tt/a(2) + b/a(2);
p1 = -a(1)*tt/a(2) + (b+1)/a(2);
p2 = -a(1)*tt/a(2) + (b-1)/a(2);

graph = plot(X(1,:),X(2,:), <span class="string">'o'</span>, Y(1,:), Y(2,:), <span class="string">'o'</span>);
set(graph(1),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'MarkerFaceColor'</span>,[0 0.5 0]);
hold <span class="string">on</span>;
plot(tt,p, <span class="string">'-r'</span>, tt,p1, <span class="string">'--r'</span>, tt,p2, <span class="string">'--r'</span>);
axis <span class="string">equal</span>
title(<span class="string">'Approximate linear discrimination via linear programming'</span>);
<span class="comment">% print -deps svc-discr.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling Mosek 9.1.9: 203 variables, 100 equality constraints
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : LO (linear optimization problem)
  Constraints            : 100             
  Cones                  : 0               
  Scalar variables       : 203             
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : LO (linear optimization problem)
  Constraints            : 100             
  Cones                  : 0               
  Scalar variables       : 203             
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the dual        
Optimizer  - Constraints            : 3
Optimizer  - Cones                  : 0
Optimizer  - Scalar variables       : 100               conic                  : 0               
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 6                 after factor           : 6               
Factor     - dense dim.             : 0                 flops                  : 1.21e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   4.0e+00  4.5e+01  2.0e+02  0.00e+00   0.000000000e+00   1.000000000e+02   4.0e+00  0.00  
1   1.6e+00  1.8e+01  7.7e+01  1.00e+00   1.326796838e+01   5.217637579e+01   1.6e+00  0.01  
2   3.4e-01  3.9e+00  1.7e+01  8.00e-01   1.276465396e+01   2.276054023e+01   3.4e-01  0.01  
3   9.4e-02  1.1e+00  4.6e+00  7.29e-01   1.056871385e+01   1.373372432e+01   9.4e-02  0.01  
4   5.7e-02  6.4e-01  2.8e+00  6.95e-01   9.981841488e+00   1.207699096e+01   5.7e-02  0.01  
5   2.4e-02  2.7e-01  1.2e+00  5.28e-01   9.577353165e+00   1.084650474e+01   2.4e-02  0.01  
6   1.5e-02  1.7e-01  7.4e-01  -2.38e-01  9.090838843e+00   1.021931758e+01   1.5e-02  0.01  
7   7.1e-03  8.0e-02  3.5e-01  7.88e-01   7.843975801e+00   8.411374355e+00   7.1e-03  0.01  
8   5.3e-03  6.0e-02  2.6e-01  3.60e-01   7.713726063e+00   8.230116821e+00   5.3e-03  0.01  
9   4.2e-03  4.7e-02  2.1e-01  7.82e-01   7.375338663e+00   7.795080822e+00   4.2e-03  0.01  
10  8.1e-04  9.2e-03  4.0e-02  9.81e-01   6.402420149e+00   6.483453549e+00   8.1e-04  0.01  
11  6.0e-04  6.7e-03  2.9e-02  7.09e-01   6.352875466e+00   6.418381327e+00   6.0e-04  0.01  
12  9.4e-05  1.1e-03  4.7e-03  9.89e-01   6.180539134e+00   6.190871435e+00   9.4e-05  0.01  
13  3.8e-07  4.3e-06  1.9e-05  9.96e-01   6.148692445e+00   6.148734520e+00   3.8e-07  0.01  
14  3.9e-11  4.4e-10  1.9e-09  1.00e+00   6.148569445e+00   6.148569449e+00   3.9e-11  0.01  
Basis identification started.
Primal basis identification phase started.
Primal basis identification phase terminated. Time: 0.00
Dual basis identification phase started.
Dual basis identification phase terminated. Time: 0.00
Basis identification terminated. Time: 0.00
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 6.1485694488e+00    nrm: 2e+01    Viol.  con: 4e-11    var: 0e+00  
  Dual.    obj: 6.1485694488e+00    nrm: 1e+00    Viol.  con: 0e+00    var: 8e-09  

Basic solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 6.1485694227e+00    nrm: 2e+01    Viol.  con: 2e-08    var: 0e+00  
  Dual.    obj: 6.1485694323e+00    nrm: 1e+00    Viol.  con: 0e+00    var: 3e-15  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 14        time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +6.14857
 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="svm_1__01.png" alt=""> 
</div>
</div>
</body>
</html>